﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HospitalSystem.Core;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class PsDetails : System.Web.UI.Page
    {

        private show_Image _showimage;
        private findchildLog _fc;
        private GroupsLog _group;
        protected void Page_Load(object sender, EventArgs e)
        {
            showsession();
            _showimage = new show_Image();
            _showimage.show_Images(cid_label, Image1);
            _fc = new findchildLog();
            _group = new GroupsLog();



            showrecord();
            showrecord2();
        }
        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            cid_label.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            lname.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            fname.Text = ShowValue3;


            String ShowValue4 = (string)Session["CID"];
            SelectStat.Text = ShowValue4;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/showcidinfo2.aspx");
            //_group.crepsnumber(HostID, StaffID);
            //_group.psnumbertolabel(psnumber);
            //sendsessionpsnumber();
            //sendsession();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/showcidinfo2.aspx");
        }


        protected void sendsessionpsnumber()
        {
            Session["PSNumber01"] = psnumber.Text;


        }


        protected void sendsession()
        {
            Session["CID"] = SelectStat.Text;
            Session["FirstName"] = fname.Text;
            Session["LastName"] = lname.Text;


            _fc.Bindcase(SelectStat, caseno);


            Session["CaseNo"] = caseno.Text;
            _fc.creDetails(SelectStat, caseno, psnumber);
            Response.Redirect("~/servicerecord2.aspx");



        }



        protected void checkcaseno()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID from Evaluation where CID = @sj", scon);
            cmd.Parameters.AddWithValue("@sj", cid_label.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {

                _group.crepsnumber(HostID, StaffID);
                _group.psnumbertolabel(psnumber);
                sendsessionpsnumber();
                sendsession();

            }

            else
            {

                MessageBox("เด็กรายได้ยังไม่ได้ทำการคัดกรองเราจะพาคุณไปหน้าทำการคัดกรอง", "cal.aspx");

            }
            reader.Close();

        }
        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }

        //ส่งเสริมการศึกษา
        protected void showrecord()
        {

            string CIDstring = Request.QueryString["CID"];
            string PSstring = Request.QueryString["PS"];

            if (CIDstring != null && PSstring != null)
            {
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                //ต้องการแก้เคสที่มีอยู่แล้ว
                scon.Open();
                SqlCommand cmd = new SqlCommand("SELECT a.[CaseNo],a.[PSNumber] as ps,a.[CID],d.ACTDesc as dec FROM [ServiceProvisionVsPerson] a join [ServiceProvisionDetails] b on a.PSNumber=b.PSNumber join [ServiceProvision] c on c.PSNumber=b.PSNumber join [Activity] d on b.ACTCode=d.ACTCode where a.CID=@CID  and b.PSNumber=@PSNumber and d.SVCCode='A0401000'", scon);
                cmd.Parameters.AddWithValue("@CID", CIDstring);
                cmd.Parameters.AddWithValue("@PSNumber", PSstring);

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                showrec.DataSource = ds;
                showrec.DataBind();
                scon.Close();

            }

            else

            { 
            
            }


        }


        protected void familylist_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
        {
            showrec.PageIndex = e.NewPageIndex;
            showrecord();
        }









       //ส่งเสริมพัฒนาการ
        protected void showrecord2()
        {

            string CIDstring = Request.QueryString["CID"];
            string PSstring = Request.QueryString["PS"];

            if (CIDstring != null && PSstring != null)
            {
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                //ต้องการแก้เคสที่มีอยู่แล้ว
                scon.Open();
                SqlCommand cmd = new SqlCommand("SELECT a.[CaseNo],a.[PSNumber] as ps,a.[CID],d.ACTDesc as dec FROM [ServiceProvisionVsPerson] a join [ServiceProvisionDetails] b on a.PSNumber=b.PSNumber join [ServiceProvision] c on c.PSNumber=b.PSNumber join [Activity] d on b.ACTCode=d.ACTCode where a.CID=@CID  and b.PSNumber=@PSNumber and d.SVCCode='A0501000'", scon);
                cmd.Parameters.AddWithValue("@CID", CIDstring);
                cmd.Parameters.AddWithValue("@PSNumber", PSstring);

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                showrec0.DataSource = ds;
                showrec0.DataBind();
                scon.Close();

            }

            else
            {

            }


        }


        protected void familylist_SelectedIndexChanged2(object sender, GridViewPageEventArgs e)
        {
            showrec0.PageIndex = e.NewPageIndex;
            showrecord2();
        }







    }
}